/*==============================================================================
** LOCNGUYEN
**= History ====================================================================
** 4.8.2012 First Create & imp
** -----------------------------------------------------------------------------
==============================================================================*/
function DataCommunication() {
    //var JSON_CALLBACK_URL = 'http://localhost:8080/Personal/SiteController?jsoncallback=?';
    var JSON_CALLBACK_URL = 'http://nguyenvanloc.jelastic.servint.net/info/SiteController?jsoncallback=?';
    // for Test
    this.handleServerEvent = handleServerEvent;
    this.addImage = addImage;
    this.getImages = getImages;
    this.getAlbums = getAlbums;
    this.addAlbum = addAlbum;
	
    function getJSON_SYNC(url, data) {
        var result;
        $.ajax({
            url : url,
            dataType : 'json',
            data : data,
            async : false,
            success : function(json) {				
                result = json;
            },
            error: function(request,status,errorThrown) {
                Utils.log(LogType.ERROR, "Request: " + request + 
                    "; status: " + status + 
                    "; Error thrown: " + errorThrown)
            }
        });
        return result;
    }

    //////////////////////////////////////////////////
    //  IMAGE {Images, album, albumDetail} service  //
    //////////////////////////////////////////////////
    /**
     * Add Image into database
     * return: all existed images
     */
    function addImage(image) {
        var json = getJSON_SYNC(JSON_CALLBACK_URL, {
            cmd : "media_add_image",
            data : JSON.stringify(image)
        });
        if (Utils.isEmpty(json))
            Utils.log(LogType.ERROR, 'Call to server to add image failed!', json);
        return json.result;
    }
    
    /**
     * Get all existed images
     * return: all existed images
     */
    function getImages() {
        var json = getJSON_SYNC(JSON_CALLBACK_URL, {
            cmd : "media_get_images"
        });
        //if (Utils.isEmpty(json))
        //    Utils.log(LogType.ERROR, 'Call to server to add image failed!', json);
        //}
        return json;
    }
    
    /**
     * Get all existed albums
     * return: all existed albums
     */
    function getAlbums() {
        var json = getJSON_SYNC(JSON_CALLBACK_URL, {
            cmd : "media_get_albums"
        });
        return json;
    }
    
    /**
     * Add new album
     * return all existed albums
     */
    function addAlbum(album) {
        var json = getJSON_SYNC(JSON_CALLBACK_URL, {
            cmd : "media_add_album",
            data : JSON.stringify(album)
        });
        if (Utils.isEmpty(json))
            Utils.log(LogType.ERROR, 'Call to server to add album failed!', json);
        return json.result;
    }
    
    function handleServerEvent(handleServerEvent, handleServerClosed) {
        
    }
}